﻿<UserControl x:Class="Samba.Modules.InventoryModule.TransactionView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Common="clr-namespace:Samba.Presentation.Common;assembly=Samba.Presentation.Common"
     mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <TextBlock Margin="5,5,0,0" Text="{Binding DateLabel}" />
            <TextBlock Margin="5,5,0,0" Text="{Binding TimeLabel}" />
            <Label>
                <LocText ResourceIdentifierKey="DocumentNo" Suffix=":"/>
            </Label>
            <TextBox HorizontalAlignment="Left" MinWidth="200" Text="{Binding Name,Mode=TwoWay}" />
            <Label>
                <LocText ResourceIdentifierKey="InventoryItems" Suffix=":" />
            </Label>
        </StackPanel>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left">
            <DataGrid ItemsSource="{Binding TransactionItems}" SelectedItem="{Binding SelectedTransactionItem, Mode=TwoWay}"
                     HorizontalAlignment="Left" GridLinesVisibility="None" RowHeaderWidth="0" Margin="0,3"
                     AutoGenerateColumns="False" CanUserSortColumns="False" CanUserDeleteRows="False" Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}"
                     PreviewKeyDown="DataGrid_PreviewKeyDown" PreviewTextInput="DataGrid_PreviewTextInput"
                     PreparingCellForEdit="DataGrid_PreparingCellForEdit">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="{LocText InventoryItemName}"
                             MinWidth="100" CellStyle="{StaticResource LeftAlignedGridCell}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3" Text="{Binding Name}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <Grid FocusManager.FocusedElement="{Binding ElementName=cb}">
                                    <ComboBox Name="cb" IsEditable="True" ItemsSource="{Binding InventoryItemNames}" SelectedItem="{Binding Name}" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="{LocText Quantity}"
                             CellStyle="{StaticResource RightAlignedGridCell}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3" Text="{Binding Path=Quantity,StringFormat='#,#0.##'}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <Grid FocusManager.FocusedElement="{Binding ElementName=tb}">
                                    <Common:FilteredTextBox Type="Number" Text="{Binding Path=Quantity,StringFormat='#,##.##'}"
                                             Name="tb" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="{LocText Unit}"
                             CellStyle="{StaticResource LeftAlignedGridCell}">
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding UnitNames}" SelectedItem="{Binding UnitName}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3" Text="{Binding UnitName}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="{LocText UnitPrice}"
                             MinWidth="70" CellStyle="{StaticResource RightAlignedGridCell}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3" Text="{Binding Path=Price,StringFormat='#,#0.00'}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <Grid FocusManager.FocusedElement="{Binding ElementName=tb1}">
                                    <Common:FilteredTextBox Type="Number" Text="{Binding Path=Price,StringFormat='#,#0.00'}"
                                             Name="tb1" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="{LocText Total}"
                             MinWidth="70" CellStyle="{StaticResource RightAlignedGridCell}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3" Text="{Binding Path=TotalPrice,StringFormat='#,#0.00'}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <Grid FocusManager.FocusedElement="{Binding ElementName=tb2}">
                                    <Common:FilteredTextBox Type="Number" Text="{Binding Path=TotalPrice,StringFormat='#,#0.00'}"
                                             Name="tb2" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
                <DataGrid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Command="{Binding AddTransactionItemCommand}" Header="{Binding AddTransactionItemCommand.Caption}" />
                        <MenuItem Command="{Binding DeleteTransactionItemCommand}" Header="{Binding DeleteTransactionItemCommand.Caption}" />
                    </ContextMenu>
                </DataGrid.ContextMenu>
            </DataGrid>
            <StackPanel Margin="10">
                <TextBlock Margin="2,3">
                        <Hyperlink Command="{Binding AddTransactionItemCommand}">
                            <TextBlock Text="{Binding Path=AddTransactionItemCommand.Caption}" />
                        </Hyperlink>
                    </TextBlock>
                <TextBlock Margin="2,3">
                        <Hyperlink Command="{Binding DeleteTransactionItemCommand}">
                            <TextBlock Text="{Binding Path=DeleteTransactionItemCommand.Caption}" />
                        </Hyperlink>
                    </TextBlock>
            </StackPanel>
        </StackPanel>
        <StackPanel Grid.Row="2">
            <Button HorizontalAlignment="Left" Margin="0,10" MinWidth="70" Content="{Binding SaveCommand.Caption}"
                 Command="{Binding SaveCommand}" />
            <Label Content="{Binding Error}" Foreground="Red" />
        </StackPanel>
    </Grid>
</UserControl>